#include "GunContra.h"


CGunContra::CGunContra()
{
	_alpha = 9.0;
}

void CGunContra::GunContra_Run(bool _isJump, bool _isRight, int _isDown, bool _isMove, int _levelBullet, float _x, float _y, CSprite *_rSprite, CSprite *_lSprite)
{
	float vx = 0, vy = 0;
	float idx = 0, idy = 0;

	#pragma region vx,vy bullet
	if (!_isJump)
	{
		if (_isRight)
		{

			if (_isDown == 1)
			{
				if (_isMove)//chia sung xeo xuong
				{
					vx = CONTRABULLETVX;
					vy = -CONTRABULLETVY;
					idx = 0;
					idy = -FRAMEWIDTH / 2;
				}
				else   //nam
				{
					vx = CONTRABULLETVX;
					vy = 0.0f;
					idx = 0;
					idy = -FRAMEWIDTH / 2;
				}
			}
			else
			{
				if (_isDown == -1)
				{
					if (_isMove)   //chia sung xeo len
					{
						vx = CONTRABULLETVX;
						vy = CONTRABULLETVY;
						idx = 0;
						idy = FRAMEWIDTH / 2;
					}
					else   //chia sung len
					{
						vx = 0.0f;
						vy = CONTRABULLETVY;
						idx = -FRAMEHEIGHT / 2 + 10;// -CONTRAWIDTH / 2 + 5;
						idy = CONTRAWIDTH / 2;
						_rSprite->Next(8, 2);
					}
				}
				else
				{
					if (_isMove)//chay ban
					{
						vx = CONTRABULLETVX;
						vy = 0.0f;
						idx = 0;
						idy = 0;
						_rSprite->Next(30, 5);
					}
					else  //dung yen
					{
						vx = CONTRABULLETVX;
						vy = 0.0f;
						idx = -16;
						idy = -3;
						_rSprite->Next(5, 2);
					}
				}
			}
		}
		else
		{
			if (_isDown == 1)
			{
				if (_isMove)
				{
					vx = -CONTRABULLETVX;
					vy = -CONTRABULLETVY;
					idx = -FRAMEHEIGHT;
					idy = -FRAMEWIDTH / 2;
				}
				else
				{
					vx = -CONTRABULLETVX;
					vy = 0.0f;
					idx = -FRAMEHEIGHT;
					idy = -FRAMEWIDTH / 2;
				}
			}
			else
			{
				if (_isDown == -1)
				{
					if (_isMove)
					{
						vx = -CONTRABULLETVX;
						vy = CONTRABULLETVY;
						idx = -FRAMEHEIGHT;
						idy = FRAMEWIDTH / 2;
					}
					else
					{
						vx = 0.0f;
						vy = CONTRABULLETVY;
						idx = -FRAMEHEIGHT / 2 - 10;
						idy = FRAMEWIDTH / 2;
						_lSprite->Next(0, 2);
					}
				}
				else
				{
					if (_isMove)//chay ban
					{
						vx = -CONTRABULLETVX;
						vy = 0.0f;
						idx = -FRAMEHEIGHT;
						idy = 0;
						_lSprite->Next(35, 5);
					}
					else   // dung yen chia sung ra
					{
						vx = -CONTRABULLETVX;
						vy = 0.0f;
						idx = -FRAMEHEIGHT;
						idy = -3;
						_lSprite->Next(3, 2);
					}
				}
			}
		}
	}
	else
	{
		if (_isDown == -1)
		{
			vx = 0.0f;
			vy = CONTRABULLETVY;
			idx = -FRAMEHEIGHT / 2;
			idy = -FRAMEWIDTH / 2;
		}
		else
		{
			if (_isDown == 1)
			{
				vx = 0.0f;
				vy = -CONTRABULLETVY;
				idx = -FRAMEHEIGHT / 2;
				idy = -FRAMEWIDTH;
			}
			else{
				if (_isRight)
				{
					vx = CONTRABULLETVX;
					vy = 0.0f;
					idx = -FRAMEHEIGHT / 2;
					idy = -FRAMEWIDTH / 2;
				}
				else
				{
					vx = -CONTRABULLETVX;
					vy = 0.0f;
					idx = -FRAMEHEIGHT;
					idy = -FRAMEWIDTH / 2;
				}
			}
		}
	}
	#pragma endregion

	int iframe = 0;// = _x - SCREEN_WIDTH / 2;
	if (_x < SCREEN_WIDTH / 2)
		iframe = 0;
	else
	{
		if (_x > GAMEWORLDWIDTH - SCREEN_WIDTH / 2)
			iframe = _x - SCREEN_WIDTH + (GAMEWORLDWIDTH - _x);
		else
			iframe = _x - SCREEN_WIDTH / 2;
	}

	if (_levelBullet != 4)
	{
		_bullet = new CBullet(_x + FRAMEWIDTH - iframe + idx, _y - FRAMEHEIGHT / 2 + idy, BULLETTIMEALIVE, vx, vy, _levelBullet);
		Add(_bullet);
	}
	else
	{
		_bullet = new CBullet(_x + FRAMEWIDTH - iframe + idx, _y - FRAMEHEIGHT / 2 + idy, BULLETTIMEALIVE, vx, vy, _levelBullet);
		Add(_bullet);


		_bullet = new CBullet(_x + FRAMEWIDTH - iframe + idx, _y - FRAMEHEIGHT / 2 + idy, BULLETTIMEALIVE, vx, vy, _levelBullet);
		Add(_bullet);

		_bullet = new CBullet(_x + FRAMEWIDTH - iframe + idx, _y - FRAMEHEIGHT / 2 + idy, BULLETTIMEALIVE, vx, vy, _levelBullet);
		Add(_bullet);

		_bullet = new CBullet(_x + FRAMEWIDTH - iframe + idx, _y - FRAMEHEIGHT / 2 + idy, BULLETTIMEALIVE, vx, vy, _levelBullet);
		Add(_bullet);

		_bullet = new CBullet(_x + FRAMEWIDTH - iframe + idx, _y - FRAMEHEIGHT / 2 + idy, BULLETTIMEALIVE, vx, vy, _levelBullet);
		Add(_bullet);
	}
}


double CGunContra::Sin(double ialpha)
{
	return sin(ialpha*PI / 180);
}

double CGunContra::Cos(double ialpha)
{
	return cos(ialpha*PI / 180);
}

void CGunContra::Draw(CSprite *isprite)
{
	CheckALiveBullet();

	if (_listBullet.size() > 0)
	{
		list<CBullet>::iterator ibullet = _listBullet.begin();

		for (ibullet; ibullet != _listBullet.end(); ibullet++)
		{
			if ((*ibullet)._levelBullet != 4)
			{
				if ((*ibullet).CheckALive())
				{
					isprite->Next((*ibullet)._levelBullet, 1);
					(*ibullet).Update();
					(*ibullet).Draw(isprite);
				}
			}
			else   //neu dan chum
			{
				if ((*ibullet).CheckALive())
				{
					if (GetTickCount() - (*ibullet)._countTime <= 300)
					{
						isprite->Next((*ibullet)._levelBullet - 1, 1);
					}
					else
					{
						if (GetTickCount() - (*ibullet)._countTime > 300 && GetTickCount() - (*ibullet)._countTime < 600)
						{
							isprite->Next((*ibullet)._levelBullet, 1);
						}
						else
						{
							isprite->Next((*ibullet)._levelBullet + 1, 1);
						}
					}

					(*ibullet).Update();
					float ix0 = (*ibullet)._x0;		// vi tri ban dau
					float iy0 = (*ibullet)._y0;
					float dx = (*ibullet)._x - ix0;	//khoang cach tu vi tri cua dan den vi tri ban dau
					float dy = (*ibullet)._y - iy0;
					(*ibullet).Draw(isprite);
					ibullet++;

					//Ban ngang
					/*(*ibullet).SetLocation(ix0 + dx * Cos( _alpha), iy0 + dx*Sin(_alpha));
					(*ibullet).Draw(isprite);*/

					//Ban thang
					/*(*ibullet).SetLocation(ix0 - dy * Sin(_alpha), iy0 + dy*Cos( _alpha));
					(*ibullet).Draw(isprite);*/

					// vi tri ban dau - ban ngang(cos,sin) + ban len(sin,cos): chay theo y thi tru theo x thi cong
					(*ibullet).SetLocation(ix0 - dy * Sin(_alpha) + dx * Cos( _alpha), iy0 + dy*Cos( _alpha) + dx*Sin(_alpha));
					(*ibullet).Draw(isprite);
					ibullet++;

					(*ibullet).SetLocation(ix0 - dy* Sin(2 * _alpha) + dx* Cos(2 * _alpha), iy0 + dy*Cos(2 * _alpha) + dx*Sin(2 * _alpha));
					(*ibullet).Draw(isprite);
					ibullet++;

					(*ibullet).SetLocation(ix0 - dy *Sin(-_alpha) + dx *Cos( -_alpha), iy0 + dy*Cos( -_alpha) + dx*Sin(-_alpha));
					(*ibullet).Draw(isprite);
					ibullet++;

					(*ibullet).SetLocation(ix0 - dy *Sin(-2 * _alpha) + dx *Cos(-2 * _alpha), iy0 + dy*Cos(-2 * _alpha) + dx*Sin(-2 * _alpha));
					(*ibullet).Draw(isprite);
				}
			}
		}
	}
}

void CGunContra::GunContra_End()
{
	if (_bullet != NULL)
		delete _bullet;

	if (_sprite != NULL)
		delete _sprite;
}

CGunContra::~CGunContra()
{
}
